using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Crystal_Skill_Controller : MonoBehaviour
{
    private Animator anim => GetComponent<Animator>();
    private CircleCollider2D cd => GetComponent<CircleCollider2D>();

    private float crystalExistTimer;//持续时间定时

    private bool canExolode;//控制爆炸
    private bool canMove;//控制移动
    private float moveSpeed;//控制速度

    private bool canGrow;//爆炸变大
    private float growSpeed = 5;//变大速度

    private Transform closestTarget;//最近的敌人
    [SerializeField] private LayerMask whatIsEnemy;//敌人层

    void Start()
    {

    }

    void Update()
    {
        crystalExistTimer -= Time.deltaTime;// 水晶存续定时器

        if (crystalExistTimer < 0)
        {
            FinishCrystal();//结束技能
        }

        //移动水晶
        if (canMove)
        {
            if (closestTarget != null)
            {
                // 移动水晶到目标
                transform.position = Vector2.MoveTowards(transform.position, closestTarget.position, moveSpeed * Time.deltaTime);
                if (Vector2.Distance(transform.position, closestTarget.position) < 1)
                {
                    FinishCrystal();
                    canMove = false;
                }
            }

        }
        //水晶爆炸时变大
        if (canGrow)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(3, 3), growSpeed * Time.deltaTime);
        }
    }

    // 造成伤害
    private void AnimationExplodeEvent()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, cd.radius);
        foreach (var hit in colliders)
        {
            // 判断敌人
            if (hit.GetComponent<Enemy>() != null)
            {
                hit.GetComponent<Entity>().SetupKnockbackDir(transform);
                PlayerManager.instance.player.stats.DoMagicalDamge(hit.GetComponent<CharacterStats>());

                ItemData_Equipment equipedAmulet = Inventory.instance.GetEquipment(EquipmentType.Amulet);
                if (equipedAmulet != null)
                {
                    equipedAmulet.ExecuteItemEffect(hit.transform);
                }
            }
        }
    }

    // 结束水晶技能
    public void FinishCrystal()
    {
        if (canExolode)//可以爆炸
        {
            canGrow = true;
            anim.SetTrigger("Explode");// 爆炸动画
        }
        else
        {
            SelfDestroy();// 自毁
        }
    }
    // 设置水晶的参数
    public void SetupCrystal(float _crystalDuration, bool _canExolode, bool _canMove, float _moveSpeed, Transform _closestTarget)
    {
        crystalExistTimer = _crystalDuration;
        canExolode = _canExolode;
        canMove = _canMove;
        moveSpeed = _moveSpeed;
        closestTarget = _closestTarget;
    }
    // 自毁
    public void SelfDestroy() => Destroy(gameObject);

    //随机选择敌人
    public void ChooseRandomEnemy()
    {
        float radius = SkillManager.instance.blackhole.GetBlackholeRadius();
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, radius, whatIsEnemy);
        if (colliders.Length > 0)
            closestTarget = colliders[Random.Range(0, colliders.Length)].transform;
    }
}
